package com.dlmz.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.dlmz.util.BaseDao;

@SuppressWarnings("serial")
public class StuAddDialog extends JDialog implements ActionListener{  
    //=========面板控件  
    //......左侧标题栏  
    private JLabel idLab,nameLab,sexLab,ageLab,deptLab,cidLab,phoneLab;  
    //......右侧信息选择填写栏  
    private JTextField idTxt,nameTxt,sexTxt,ageTxt,deptTxt,cidTxt,phoneTxt;
    //......添加和取消按钮  
    private JButton addBtn,cancelBtn;  
    //......布局控件  
    private JPanel left,center,bottom;  
      
    //构造函数  
    public StuAddDialog(JFrame owner, String title, boolean modal)   
    {  
        //========重写父类方法  
        super(owner, title, modal);  
        //========左侧标签栏  
        idLab = new JLabel("学号: ");  
        nameLab = new JLabel("姓名: ");  
        sexLab = new JLabel("性别: ");  
        ageLab = new JLabel("年龄: ");  
        deptLab = new JLabel("系别: ");  
        cidLab = new JLabel("班级: ");
        phoneLab = new JLabel("联系方式：");
        //========右侧信息填写栏  
        idTxt = new JTextField();  
        nameTxt = new JTextField();  
        sexTxt = new JTextField();  
        ageTxt = new JTextField();  
        deptTxt = new JTextField();  
        cidTxt = new JTextField();
        phoneTxt = new JTextField();
        //========添加和取消按钮  
        addBtn = new JButton("添加");  
        cancelBtn = new JButton("取消");  
        //......添加监听  
        addBtn.addActionListener(this);  
        addBtn.setActionCommand("add");  
        cancelBtn.addActionListener(this);  
        cancelBtn.setActionCommand("cancel");  
        //========创建布局  
        //......创建左边栏  
        left = new JPanel();  
        left.setLayout(new GridLayout(7, 1));  
        left.add(idLab);  left.add(nameLab);   
        left.add(sexLab); left.add(ageLab);   
        left.add(deptLab);  left.add(cidLab); 
        left.add(phoneLab); 
        //......创建右边栏  
        center = new JPanel();  
        center.setLayout(new GridLayout(7, 1));  
        center.add(idTxt);  center.add(nameTxt);  
        center.add(sexTxt); center.add(ageTxt);  
        center.add(deptTxt);  center.add(cidTxt); 
        center.add(phoneTxt); 
        //========底层添加和取消按钮  
        bottom = new JPanel();  
        bottom.add(addBtn);  
        bottom.add(cancelBtn);  
        //========整体布局  
        this.add(left,BorderLayout.WEST);  
        this.add(center,BorderLayout.CENTER);  
        this.add(bottom,BorderLayout.SOUTH);  
        //========设置窗口属性  
          
        validate();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(450,300);
		this.setLocationRelativeTo(null);
		setVisible(true);   
    }  
  
    public void actionPerformed(ActionEvent e)   
    {  
        // TODO Auto-generated method stub  
        if(e.getActionCommand().equals("add")) {  
            /***********************添加学生信息**************************/  
            StuModel tmp = new StuModel();  
            String sql = "insert into Student values(?,?,?,?,?,?,?)";  
            int sno =Integer.parseInt(idTxt.getText());
            String sname=nameTxt.getText();
            String ssex=sexTxt.getText();
            int sage=Integer.parseInt(ageTxt.getText());
            String sdept=deptTxt.getText();
            String sclass=cidTxt.getText();
            String sphone=phoneTxt.getText();
            BaseDao basedao=new BaseDao();
			int num=basedao.update(sql, sno,sname,ssex,sage,sdept,sclass,sphone);
			if(num>0) {
				JOptionPane.showMessageDialog(null, "添加学生信息成功！");
				this.dispose(); 
			}else {
				JOptionPane.showMessageDialog(null, "添加学生信息失败！");
			}
            //========关闭窗口  
            this.dispose();  
        } else if(e.getActionCommand().equals("cancel")) {  
            //========关闭窗口  
            this.dispose();  
        }  
    }
}  

